package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsProduct;
import com.caiyi.financial.nirvana.sms.querybean.QueryProductBean;
import com.caiyi.financial.nirvana.sms.vo.SmsProductByParamVo;
import com.caiyi.financial.nirvana.sms.vo.SmsUserProductListVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsProductMapper extends BaseMapper<SmsProduct> {

    @SelectProvider(type = ProductSqlProvider.class, method = "getUserProductParams")
    List<SmsUserProductListVo> getUpdateUserProductParams();

    @Update("Update sms_user set product_id=#{id} where account=#{account}")
    int updateUserProduct(@Param("id") Integer id, @Param("account") String account);

    @SelectProvider(type = ProductSqlProvider.class, method = "selectProductListByParams")
    List<SmsProductByParamVo> selectProductListByParams(QueryProductBean bean);

    class ProductSqlProvider {
        public String selectProductListByParams(QueryProductBean bean) {
            return new SQL() {
                {
                    SELECT("id,name,( SELECT GROUP_CONCAT( NAME ORDER BY FIND_IN_SET( id, yd_gateways ) ) FROM sms_gateway WHERE FIND_IN_SET( id, yd_gateways ) ) AS ydGatewayName," +
                            "( SELECT GROUP_CONCAT( NAME ORDER BY FIND_IN_SET( id, lt_gateways ) ) FROM sms_gateway WHERE FIND_IN_SET( id, lt_gateways ) ) AS ltGatewayName," +
                            "( SELECT GROUP_CONCAT( NAME ORDER BY FIND_IN_SET( id, dx_gateways ) ) FROM sms_gateway WHERE FIND_IN_SET( id, dx_gateways ) ) AS dxGatewayName," +
                            "lt_gateways as ltGatewayId,dx_gateways as dxGatewayId,yd_gateways as ydGatewayId,create_time as createTime,update_time as updateTime,description ");
                    FROM("sms_product");
                    if (!StringUtils.isEmpty(bean.getName())) {
                        WHERE("name LIKE concat('%',#{name},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }

        public String getUserProductParams() {
            return new SQL() {
                {
                    SELECT(" id ,name,(SELECT GROUP_CONCAT(NAME ORDER BY FIND_IN_SET(id, yd_gateways)) FROM sms_gateway WHERE FIND_IN_SET(id, yd_gateways)) AS ydGatewayName," +
                            "(SELECT GROUP_CONCAT(NAME ORDER BY FIND_IN_SET(id, lt_gateways)) FROM sms_gateway WHERE FIND_IN_SET(id, lt_gateways)) AS ltGatewayName," +
                            "(SELECT GROUP_CONCAT(NAME ORDER BY FIND_IN_SET(id, dx_gateways)) FROM sms_gateway WHERE FIND_IN_SET(id, dx_gateways)) AS dxGatewayName");
                    FROM("sms_product");
                    GROUP_BY("id");

                }
            }.toString();
        }


    }
}
